package vn.com.fis.portal.vtcc.service.persistence;

import java.util.ArrayList;
import java.util.List;

import vn.com.fis.portal.vtcc.model.SampleEntry;
import vn.com.fis.portal.vtcc.model.StrainEntry;
import vn.com.fis.portal.vtcc.model.impl.SampleEntryImpl;
import vn.com.fis.portal.vtcc.model.impl.StrainEntryImpl;

import com.liferay.portal.kernel.dao.orm.QueryPos;
import com.liferay.portal.kernel.dao.orm.QueryUtil;
import com.liferay.portal.kernel.dao.orm.SQLQuery;
import com.liferay.portal.kernel.dao.orm.Session;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
import com.liferay.util.dao.orm.CustomSQLUtil;

public class SampleEntryFinderImpl extends BasePersistenceImpl<SampleEntry>
		implements SampleEntryFinder {
	private static Log _log = LogFactoryUtil.getLog(SampleEntryFinderImpl.class);
	
	final  String  FINDBY_SN_SS_SC_CY_ON_SOC_SI_H_IM_IT_DT_IP_CP_OC_A_STRAINID = SampleEntryFinderImpl.class.getName() +".findBy_SN_SS_SC_CY_ON_SOC_SI_H_IM_IT_DT_IP_CP_OC_A_STRAINID";
	final  String  COUNTBY_SN_SS_SC_CY_ON_SOC_SI_H_IM_IT_DT_IP_CP_OC_A_STRAINID = SampleEntryFinderImpl.class.getName() +".countBy_SN_SS_SC_CY_ON_SOC_SI_H_IM_IT_DT_IP_CP_OC_A_STRAINID";
	
	final  String  FINDBY_SN_SS_SC_CY_ON_SOC_SI_H_IM_IT_DT_IP_CP_OC_A = SampleEntryFinderImpl.class.getName() +".findBy_SN_SS_SC_CY_ON_SOC_SI_H_IM_IT_DT_IP_CP_OC_A";
	final  String  COUNTBY_SN_SS_SC_CY_ON_SOC_SI_H_IM_IT_DT_IP_CP_OC_A = SampleEntryFinderImpl.class.getName() +".countBy_SN_SS_SC_CY_ON_SOC_SI_H_IM_IT_DT_IP_CP_OC_A";
	
	/**
	 * @findBy_SN_SS_SC_CY_ON_SOC_SI_H_IM_IT_DT_IP_CP_OC_A_STRAINID()
	 * @param scientificName
	 * @param sampleSynonym
	 * @param sampleCode
	 * @param catalogYear
	 * @param otherName
	 * @param sampleOtherCode
	 * @param sourceIsolation
	 * @param history
	 * @param isolationMethod
	 * @param isolationTime
	 * @param depositionTime
	 * @param isolationPerson
	 * @param cellMorphology
	 * @param otherCharacteristics
	 * @param applications
	 * @param strainId int value
	 * @andOperator boolean 
	 * @andOperator start
	 * @andOperator end
	 * @return List<SampleEntry>
	 * 
	 */
	public List<SampleEntry> findBy_SN_SS_SC_CY_ON_SOC_SI_H_IM_IT_DT_IP_CP_OC_A_STRAINID(
								String scientificName ,String sampleSynonym ,String sampleCode, String catalogYear,
								String otherName,String sampleOtherCode,String sourceIsolation,String history,
								String isolationMethod,String isolationTime,String depositionTime,String isolationPerson,
								String cellMorphology,String otherCharacteristics,String applications,int strainId,
								boolean andOperator , int start, int end) throws SystemException {
		Session session = null;
		List<SampleEntry> sampleEntries = new ArrayList<SampleEntry>();
		try {
			session = openSession();
			String sql="";
			if(strainId>0){
				  sql = CustomSQLUtil.get(FINDBY_SN_SS_SC_CY_ON_SOC_SI_H_IM_IT_DT_IP_CP_OC_A_STRAINID);
			}
			else{ 
				  sql = CustomSQLUtil.get(FINDBY_SN_SS_SC_CY_ON_SOC_SI_H_IM_IT_DT_IP_CP_OC_A);
			}
			
			sql = CustomSQLUtil.replaceAndOperator(sql, andOperator); // AND
			SQLQuery query = session.createSQLQuery(sql);
			query.addEntity(SampleEntryImpl.TABLE_NAME, SampleEntryImpl.class);
			QueryPos qPos = QueryPos.getInstance(query);
			
			// add param 
			
			qPos.add("%"+scientificName+"%");
			qPos.add(scientificName);
			
			qPos.add("%"+sampleSynonym+"%");
			qPos.add(sampleSynonym);
			
			qPos.add("%"+sampleCode+"%");
			qPos.add(sampleCode);
			
			qPos.add("%"+catalogYear+"%");
			qPos.add(catalogYear);
			
			qPos.add("%"+otherName+"%");
			qPos.add(otherName);
			
			qPos.add("%"+ sampleOtherCode+"%");
			qPos.add( sampleOtherCode);
			
			qPos.add("%"+sourceIsolation +"%");
			qPos.add(sourceIsolation );
			
			qPos.add("%"+history +"%");
			qPos.add( history);
			
			qPos.add("%"+isolationMethod +"%");
			qPos.add(isolationMethod );
			
			qPos.add("%"+isolationTime +"%");
			qPos.add(isolationTime );
			
			qPos.add("%"+depositionTime +"%");
			qPos.add(depositionTime );
			
			qPos.add("%"+  isolationPerson+"%");
			qPos.add(  isolationPerson);
			
			qPos.add("%"+ cellMorphology +"%");
			qPos.add( cellMorphology );
			
			qPos.add("%"+ otherCharacteristics +"%");
			qPos.add(otherCharacteristics  );
			
			qPos.add("%"+ applications +"%");
			qPos.add( applications );
			
			if(strainId>0){
				qPos.add("%"+strainId+"%");
				qPos.add(strainId);
			}
			 
			sampleEntries = (List<SampleEntry>) QueryUtil.list(query, getDialect(), start,end);
			/*q.addScalar(COUNT_COLUMN_NAME, Type.LONG);

			Iterator<Long> itr = q.iterate();

			if (itr.hasNext()) {
				Long count = itr.next();

				if (count != null) {
					return count.intValue();
				}
			}*/

			/*Object  obj = query.uniqueResult();
			count = Integer.valueOf(obj.toString());
			*/
			
		}
		catch (Exception e) {
			//throw new SystemException(e);
			_log.error(e,e);
		}
		finally {
			closeSession(session);
		}
		return sampleEntries;
	}
	
	/**
	 * @countBy_SN_SS_SC_CY_ON_SOC_SI_H_IM_IT_DT_IP_CP_OC_A_STRAINID
	 * @param scientificName
	 * @param sampleSynonym
	 * @param sampleCode
	 * @param catalogYear
	 * @param otherName
	 * @param sampleOtherCode
	 * @param sourceIsolation
	 * @param history
	 * @param isolationMethod
	 * @param isolationTime
	 * @param depositionTime
	 * @param isolationPerson
	 * @param cellMorphology
	 * @param otherCharacteristics
	 * @param applications
	 * @param strainId int value
	 * @andOperator boolean 
	 * @return List<SampleEntry>
	 * 
	 */

	public int  countBy_SN_SS_SC_CY_ON_SOC_SI_H_IM_IT_DT_IP_CP_OC_A_STRAINID(
						String scientificName ,String sampleSynonym ,String sampleCode, String catalogYear,
						String otherName,String sampleOtherCode,String sourceIsolation,String history,
						String isolationMethod,String isolationTime,String depositionTime,String isolationPerson,
						String cellMorphology,String otherCharacteristics,String applications,int strainId,
						boolean andOperator  ) throws SystemException {
			Session session = null;
			int count=0;
			try {
			session = openSession();
			String sql="";
			if(strainId>0){
			sql = CustomSQLUtil.get(COUNTBY_SN_SS_SC_CY_ON_SOC_SI_H_IM_IT_DT_IP_CP_OC_A_STRAINID);
			}
			else{ 
			sql = CustomSQLUtil.get(COUNTBY_SN_SS_SC_CY_ON_SOC_SI_H_IM_IT_DT_IP_CP_OC_A);
			}
			
			sql = CustomSQLUtil.replaceAndOperator(sql, andOperator); // AND
			SQLQuery query = session.createSQLQuery(sql);
			//query.addEntity(SampleEntryImpl.TABLE_NAME, SampleEntryImpl.class);
			QueryPos qPos = QueryPos.getInstance(query);
			
			// add param 
			
			qPos.add("%"+scientificName+"%");
			qPos.add(scientificName);
			
			qPos.add("%"+sampleSynonym+"%");
			qPos.add(sampleSynonym);
			
			qPos.add("%"+sampleCode+"%");
			qPos.add(sampleCode);
			
			qPos.add("%"+catalogYear+"%");
			qPos.add(catalogYear);
			
			qPos.add("%"+otherName+"%");
			qPos.add(otherName);
			
			qPos.add("%"+ sampleOtherCode+"%");
			qPos.add( sampleOtherCode);
			
			qPos.add("%"+sourceIsolation +"%");
			qPos.add(sourceIsolation );
			
			qPos.add("%"+history +"%");
			qPos.add( history);
			
			qPos.add("%"+isolationMethod +"%");
			qPos.add(isolationMethod );
			
			qPos.add("%"+isolationTime +"%");
			qPos.add(isolationTime );
			
			qPos.add("%"+depositionTime +"%");
			qPos.add(depositionTime );
			
			qPos.add("%"+  isolationPerson+"%");
			qPos.add(  isolationPerson);
			
			qPos.add("%"+ cellMorphology +"%");
			qPos.add( cellMorphology );
			
			qPos.add("%"+ otherCharacteristics +"%");
			qPos.add(otherCharacteristics  );
			
			qPos.add("%"+ applications +"%");
			qPos.add( applications );
			
			if(strainId>0){
			qPos.add("%"+strainId+"%");
			qPos.add(strainId);
			}
			
		//	sampleEntries = (List<SampleEntry>) QueryUtil.list(query, getDialect(), start,end);
			/*q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
			
			Iterator<Long> itr = q.iterate();
			
			if (itr.hasNext()) {
			Long count = itr.next();
			
			if (count != null) {
			return count.intValue();
			}
			}*/
			
			Object  obj = query.uniqueResult();
			count = Integer.valueOf(obj.toString());
			
			}
			catch (Exception e) {
			//throw new SystemException(e);
			_log.error(e,e);
			}
			finally {
			closeSession(session);
			}
		return count;
		}
}
